#include "cadcurtain.h"

static QStringList LIST_OPEN = (QStringList()  << "开" << "打开" << "拉开" );
static QStringList LIST_CLOSE = (QStringList() << "关" << "闭" << "合" );
static QStringList LIST_STOP = (QStringList() << "停" );
static QStringList LIST_NAME = (QStringList() << "窗帘" << "纱帘" << "卷帘" << "布帘" << "遮阳帘" << "百叶窗" << "电动窗" );
static QStringList LIST_NAME_ALL = (QStringList() << "窗");

CADCurtain::CADCurtain(QObject *parent):CAudioControlDevice(parent)
{

}

void CADCurtain::addDevice(DEVICE_OPERATION *d)
{
    _device_list.append(d);
}

void CADCurtain::clearDevice()
{
    foreach (DEVICE_OPERATION *d, _device_list) {
        d->deleteLater();
    }

    _device_list.clear();
}

DEVICE_OPERATION * CADCurtain:: parseWord(QString word)
{
    QString op,name,value;
    bool action_all = false;

    //查找项目中的匹配设备
    foreach (DEVICE_OPERATION *s, _device_list) {
        if(word.contains(s->_name))
        {
            name = s->_name;
            break;
        }
    }

    //匹配统称
    if(name.isEmpty())
    {
        foreach (QString s, LIST_NAME_ALL) {
            if(word.contains(s))
            {
                name = s;
                action_all = true;
                break;
            }
        }
    }

    foreach (QString s, LIST_OPEN) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_MOVE;
            value = DEVICE_STATUS_ON;
            break;
        }
    }

    foreach (QString s, LIST_CLOSE) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_MOVE;
            value = DEVICE_STATUS_OFF;
            break;
        }
    }

    foreach (QString s, LIST_STOP) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_MOVE;
            value = DEVICE_STATUS_STOP;
            break;
        }
    }

    if(name.isEmpty() || op.isEmpty() || value.isEmpty())
    {
        return nullptr;
    }

    qDebug()<< "Audio: found device " << name;
    DEVICE_OPERATION *device_op = new DEVICE_OPERATION();
    device_op->_audio_name = name;
    device_op->_audio_operation = op;
    device_op->_audio_value = value;
    device_op->_audio_action_dev_all = action_all;

    return device_op;
}


bool CADCurtain::actionAudionControl(DEVICE_OPERATION *devOp)
{
    devOp->_match = false;

    foreach (DEVICE_OPERATION *d, _device_list) {
        if(devOp->_audio_action_dev_all)
        {
            devOp->_id_all << d->_id;
        }
        else if(d->_name.contains(devOp->_audio_name) )
        {
            devOp->_id = d->_id;
            devOp->_name = d->_name;
            devOp->_match = true;

            return true;
        }
    }

    if(devOp->_audio_action_dev_all && devOp->_id_all.size() >0)
    {
        devOp->_name = "all";
        devOp->_match = true;
        return true;
    }

    return false;
}
